﻿#include "mylistwidgetitem2.h"
#include "ui_mylistwidgetitem2.h"
#include "chatsocket.h"

MyListWidgetItem2::MyListWidgetItem2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyListWidgetItem2)
{
    ui->setupUi(this);
    ui->label_status->hide();
    ui->btn_agree->raise();
    ui->btn_refuse->raise();
}

MyListWidgetItem2::~MyListWidgetItem2()
{
    delete ui;
}

void MyListWidgetItem2::initAll(UserInfo uInfo,QString msg)
{
    m_userInfo = uInfo;
    ui->btn_head->setStyleSheet(QString("min-width:  60px;max-width:  60px;min-height: 60px;max-height: 60px;border-radius: 30px;border-width: 1 1 1 1;border-image: url(%1) 0 0 0 0 stretch strectch;").arg(uInfo.headurl));
    ui->label_msg->setText("验证消息: " + msg);
    ui->label_nickname->setText(uInfo.nickname);
}

void MyListWidgetItem2::setLableStuats(int type)
{
    if(type == 0)//已同意好友请求
    {
        ui->btn_refuse->hide();
        ui->btn_agree->hide();
        ui->label_status->raise();
        ui->label_status->show();
        ui->label_status->setText("您已同意该好友请求");
    }
    else if(type == 1)
    {
        ui->btn_refuse->hide();
        ui->btn_agree->hide();
        ui->label_status->raise();
        ui->label_status->show();
        ui->label_status->setText("您已拒绝该好友请求");
    }
    else if(type == 2)
    {
        ui->btn_refuse->hide();
        ui->btn_agree->hide();
        ui->label_status->raise();
        ui->label_status->show();
        ui->label_status->setText("您已发送好友请求,等待对方同意中...");
    }
    else if(type == 3)
    {
        ui->btn_refuse->hide();
        ui->btn_agree->hide();
        ui->label_status->raise();
        ui->label_status->show();
        ui->label_status->setText("对方拒绝了您的好友请求");
    }
    else if(type == 4)
    {
        ui->btn_refuse->hide();
        ui->btn_agree->hide();
        ui->label_status->raise();
        ui->label_status->show();
        ui->label_status->setText("对方同意了您的好友请求");
    }
}

QString MyListWidgetItem2::getID()
{
    return m_userInfo.id;
}

void MyListWidgetItem2::on_btn_agree_clicked()//同意
{
    //修改文件中的状态
    QString filepath;
    QString str = QCoreApplication::applicationDirPath();
    QString id = ChatSocket::getInstance()->getUserID();
    filepath = QString("%1/%2/friendRequest.json").arg(str).arg(id);
    FILE* fp = NULL;
    fp = fopen(filepath.toStdString().c_str(),"r");
    if(!fp)return;
    Json::Value root;
    Json::Reader reader;
    fseek(fp,0,SEEK_END);
    int len = ftell(fp);
    fseek(fp,0,SEEK_SET);
    char* buff = new char[len+1];
    fread(buff,len,1,fp);
    buff[len] = 0;
    if(reader.parse(buff,root))
    {
        for(int i = 0; i < root.size();i++)
        {
            QString sender = root[i]["Sender"].asCString();
            if(sender == m_userInfo.id)
            {
                root[i]["Status"] = 0;
                setLableStuats(0);
                break;
            }
        }
    }
    fclose(fp);
    fp = NULL;
    fp = fopen(filepath.toStdString().c_str(),"w");
    if(fp)
    {
        std::string content = root.toStyledString();
        fwrite(content.c_str(),strlen(content.c_str()),1,fp);
        fflush(fp);
        fclose(fp);
    }
    delete [] buff;
    buff = nullptr;
    //发送同意请求
    Json::Value value;
    value["TYPE"] = ADD_FRIEND_S;
    value["Recv"] = m_userInfo.id.toStdString();
    value["Sender"] = id.toStdString();
    ChatSocket::getInstance()->sendChatData(value);
}

void MyListWidgetItem2::on_btn_refuse_clicked()//拒绝
{
    //修改文件中的状态
    QString filepath;
    QString str = QCoreApplication::applicationDirPath();
    QString id = ChatSocket::getInstance()->getUserID();
    filepath = QString("%1/%2/friendRequest.json").arg(str).arg(id);
    FILE* fp = NULL;
    fp = fopen(filepath.toStdString().c_str(),"r");
    if(!fp)return;
    Json::Value root;
    Json::Reader reader;
    fseek(fp,0,SEEK_END);
    int len = ftell(fp);
    fseek(fp,0,SEEK_SET);
    char* buff = new char[len+1];
    fread(buff,len,1,fp);
    buff[len] = 0;
    if(reader.parse(buff,root))
    {
        for(int i = 0; i < root.size();i++)
        {
            QString sender = root[i]["Sender"].asCString();
            if(sender == m_userInfo.id)
            {
                root[i]["Status"] = 1;
                setLableStuats(1);
                break;
            }
        }
    }
    fclose(fp);
    fp = NULL;
    fp = fopen(filepath.toStdString().c_str(),"w");
    if(fp)
    {
        std::string content = root.toStyledString();
        fwrite(content.c_str(),strlen(content.c_str()),1,fp);
        fflush(fp);
        fclose(fp);
    }
    delete [] buff;
    buff = nullptr;
    //发送拒绝请求
    Json::Value value;
    value["TYPE"] = ADD_FRIEND_F;
    value["Recv"] = m_userInfo.id.toStdString();
    value["Sender"] = id.toStdString();
    ChatSocket::getInstance()->sendChatData(value);
}

